package com.user.notiy.pool;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.plat.common.UmsConfig;
import com.ums.umsmsg.UmsMsg;
import com.ums.umsmsg.proc.Trans;
import com.user.notiy.dao.TNoticeMapper;

public class NewQRNotice extends NoticeThread {
	private static Logger log = LoggerFactory.getLogger(NewQRNotice.class);
	private static final long SLEEP_TIME = 1000;
	private static final String CFG_FILE = "Notice.xml";
	private static final String CFG_DIR = UmsConfig.getProperty("msg", "cfg.dir");

	/**
	 * @Func	NoticeThread
	 * @Brief	构造函数
	 */
	public NewQRNotice(String name, String typeID, String groupID, TNoticeMapper tNoticeDAO) {
		super(name, typeID, groupID, tNoticeDAO);
	}
	
	private void addSign(Map outMap){
		try {
			log.info("开始加签名");
			List<Map<String, Object>> inMerKey = tNoticeDAO.getInMerKey(outMap.get("merId").toString());
			if(inMerKey == null || inMerKey.isEmpty() || inMerKey.size()>=1){
				inMerKey = tNoticeDAO.getInOrgKey(outMap.get("orgId").toString());
			}
			if(inMerKey == null || inMerKey.isEmpty() || inMerKey.size()<1){
				return;
			}
			
			outMap.put("signature", getSign(inMerKey.get(0),outMap));
		} catch (Exception e) {
			log.error("组签名失败["+e+"]");
		}
	
	}
	public String getSign(Map signData,Map outMap){
		log.info("签名数据获取完成，开始执行获取签名");
		try {
			byte[] signMsg = null;
			
			return new String(signMsg);
		} catch (Exception e) {
			log.error("获取签名异常["+e+"]");
			return "";
		}
	}

	/**
	 * @Func	pack
	 * @Brief	组包
	 */
	protected byte[] pack(Map<String, String> map) {
		addSign(map);
		
		return UmsMsg.pack(CFG_DIR + trans.getCFG(Trans.IN), map);
	}

	/**
	 * @Func	unpack
	 * @Brief	解包
	 */
	@SuppressWarnings("unchecked")
	protected Map<String, String> unpack(byte[] rspMsg) {
		Map unpack = UmsMsg.unpack(CFG_DIR + trans.getCFG(Trans.IN), rspMsg);
		if(unpack != null){
			if(!unpack.get("success").equals("0")){
				return null;
			}
		}
		return unpack;
	}
	
	/**
	 * @Func	mapp
	 * @Brief	映射
	 */
	protected Map<String, String> mapp(Map<String, String> map) {
		Map<String, String> newMap = new HashMap<String, String>();
		UmsMsg.mapping(CFG_DIR + trans.getCFG("MAPP"), map, newMap);
		return newMap;
	}
	
	/**
	 * @Func	send
	 * @Brief	发送
	 */
	protected byte[] send(String url,byte[] reqMsg) {
		log.info("发送通知url["+url+"],通知数据["+new String(reqMsg)+"]");
		String sendPost = "";
		log.info("通知返回数据["+sendPost+"]");
		return sendPost.getBytes();
	}

	@Override
	protected String getURL(Map<String, String> map) {
		log.info("开始获取URL");
		String string = map.get("NoticeURL");
		log.info("开始获取URL["+string+"]");
		return string;
	}
}
